package com.goldsprite.microhorigame;

import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.utils.viewport.*;
import com.goldsprite.gameframeworks.assets.*;
import com.goldsprite.gameframeworks.input.GameInputSystem;
import com.goldsprite.gameframeworks.screens.*;
import com.goldsprite.gameframeworks.utils.math.*;
import com.goldsprite.microhorigame.screens.*;
import com.goldsprite.microhorigame.screens.basics.*;
import com.goldsprite.microhorigame.screens.editors.*;
import com.goldsprite.gameframeworks.log.*;

public class GdxLauncher extends ApplicationAdapter {
	public static final float WORLD_WIDTH = 960;
	public static final float WORLD_HEIGHT = 540;
	public static boolean showFps = true;

	private SpriteBatch batch;
	private BitmapFont font;
	OrthographicCamera cam;

	private InputMultiplexer imp, screensImp;

	private Stage stage;
	private FitViewport viewport;

	@Override
	public void create() {
		//实例化全局资源
		new GlobalAssets();

		imp = new InputMultiplexer();
		Gdx.input.setInputProcessor(imp);

		//初始化输入系统
		imp.addProcessor(GameInputSystem.getInstance());

		//创建统一视口
		viewport = new FitViewport(WORLD_WIDTH, WORLD_HEIGHT);
		viewport.apply(true);

		//初始化屏幕
		screensImp = new InputMultiplexer();
		imp.addProcessor(screensImp);
		new ScreenManager(screensImp).
			setViewport(viewport).
			addScreen(new StartScreen()).
			addScreen(new MapEditorScreen()). // 添加Unity风格地图编辑器
			addScreen(new ExampleSelectScreen()).
			setLaunchScreen(StartScreen.class);

		batch = new SpriteBatch();
		font = FontUtils.generateAutoClarity(13);

		cam = (OrthographicCamera) ScreenManager.getInstance().getViewport().getCamera();

		//初始化调试舞台
		stage = new Stage(viewport);
		imp.addProcessor(stage);

		Debug.getInstance().initRootLayout(stage);
	}

	@Override
	public void render() {

		GameInputSystem.getInstance().update();

		ScreenManager.getInstance().render();

		if (showFps) drawFps();

		//绘制调试舞台
		stage.act();
		stage.draw();
	}

	private final GlyphLayout layout = new GlyphLayout();
	private void drawFps() {
		batch.setProjectionMatrix(cam.combined);
		batch.begin();

		// 在渲染方法中
		try (Vector2 mouseWorldPos = Vector2.obtain(Gdx.input.getX(), Gdx.input.getY())) {
			GScreen curScreen = (GScreen) ScreenManager.getInstance().getCurScreen();
			curScreen.screenToWorldCoord(mouseWorldPos);

			String[] lines = {
				"FPS: " + Gdx.graphics.getFramesPerSecond(),
				"GameVersion: " + BuildConfig.DEV_VERSION,
			};

			float margin = 10;
			float yMargin = 5;
			float lineMargin = 5f; // 行间距
			float viewportWidth = viewport.getWorldWidth();
			float lineHeight = font.getLineHeight();

			// 计算总高度（包括行间距）
			float totalHeight = (lines.length * lineHeight) + ((lines.length - 1) * lineMargin);
			float y = yMargin + totalHeight; // 从底部开始

			// 为每行单独计算位置
			for (int i = 0; i < lines.length; i++) {
				layout.setText(font, lines[i]);

				float x = viewportWidth - layout.width - margin;
				float currentY = y - (i * (lineHeight + lineMargin));

				font.draw(batch, lines[i], x, currentY);
			}
		}

		batch.end();
	}

	@Override
	public void resize(int width, int height) {
		viewport.update(width, height, true);
		ScreenManager.getInstance().resize(width, height);
	}

	@Override
	public void dispose() {
		ScreenManager.getInstance().dispose();
	}

}
